<?php
/**
 * Az osztály rövid leírása
 *
 * Az osztály hosszú leírása, példakód
 * akár több sorban is
 *
 * @package
 * @author gabi
 * @since 2014.03.31. 16:23
 */
class TaskManager
{
    /** @var TaskCollection */
    protected $pendingTasks;

    /** @var DIContainer */
    protected $container;

    public function __construct(DIContainer $container)
    {
        $this->container = $container;
    }

    /**
     * @return \TaskCollection
     */
    public function getPendingTasks()
    {
        return $this->pendingTasks;
    }

    /**
     * @param \TaskCollection $pendingTasks
     */
    public function setPendingTasks(TaskCollection $pendingTasks)
    {
        $this->pendingTasks = $pendingTasks;
    }

    public function execute()
    {
        foreach ($this->pendingTasks as $task) {
            /**@var $task Task */
            switch ($task->getType()) {
                case 'Email':
                    if (
                        preg_match(
                            '/^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)+$/',
                            $task->getAddress()
                        )
                    ) {
                        $this->container->get('EmailService')->sendTask($task);
                    }
                    break;
                case 'Sms':
                    $this->container->get('PhoneService')->sendMsg($task);
                    break;
                case 'Cron':
                    $task->runCommand();
                    break;
            }
        }
    }

}
